<template>
    <div v-if="tkIsShow">
        <div class="zhezhao"></div>
        <div class="tankuang">
            <div class="tk_title">{{ title }}</div>
            <img src="../assets/icon-close.png" class="tk_close" @click="closeTk">
            <div class="tk_content">
                <div class="tk_top_btn_box"></div>
                <el-select v-model="type" placeholder="请选择" size="small" @change="handleSelectChange">
                    <el-option label="商品图" value="1"></el-option>
                    <el-option label="商品详情图" value="2"></el-option>
                    <el-option label="弹出框图片" value="3"></el-option>
                    <el-option label="编辑页正面" value="4"></el-option>
                    <el-option label="编辑页反面" value="5"></el-option>
                    <el-option label="展示页正面" value="6"></el-option>
                    <el-option label="展示页反面" value="7"></el-option>
                    <el-option label="pdf封面正面" value="8"></el-option>
                    <el-option label="pdf封面反面" value="9"></el-option>
                    <el-option label="默认背面模板" value="10"></el-option>
                </el-select>
                <el-button type="primary" @click="edit(0,'add')" class="addBTN" size="small">添加图片</el-button>
                <el-table
                        :data="tableData"
                        style="width: 100%;"
                        height="90%">
                        <el-table-column
                        label="图片"
                        width="200">
                        <template slot-scope="scope">
                        <el-image
                            style="width: 100px; height: 100px"
                            :src="scope.row.picUrl"
                            fit="fill" ></el-image>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="level"
                        label="排序（数字越大越靠前）"
                        width="200">
                        </el-table-column>
                        <el-table-column
                        label="操作"
                        width="250">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.id,'edit')" type="text" size="medium">编辑</el-button>
                            <el-button @click="openTips(scope.row.id)" type="text" size="medium" style="color: #FF0101;">删除</el-button>
                        </template>
                        </el-table-column>
                    </el-table>
            </div>
        </div>
        <tips-box
          ref="tipsBox"
          @confirmTip="confirmTip"
        ></tips-box>
        <add-goodsPic
        ref="addGoodsPic"
        @close="
            getData()
        "
        ></add-goodsPic>
    </div>
</template>
<script>
import tipsBox from "@/components/tipsBox.vue";
import addGoodsPic from "@/components/addGoodsPic.vue"
export default {
    components: {
    tipsBox,
    addGoodsPic
  },
  data() {
    return {
        tkIsShow:false,
        title:"",
        tableData:[],
        spuId:0,
        type:"1",
        editForm:{
            id:0
        }
    };
  },
  methods: {
    __init(id) {
      this.spuId=id;
      this.getData();
    },
    getData(){
        var url="manager/getGoodsImageList";
        var params={
            id: this.spuId,
            type: this.type
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
                this.tableData=res.data;
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    closeTk(){
        this.tkIsShow = false;
    },
    openTips(id){
        this.editForm={
            id:id
        }
        var tipContent="确定要删除此图片吗？";
        if (this.$refs.tipsBox) {
            // 显示当前详情
            this.$refs.tipsBox.tkIsShow = true;
            this.$refs.tipsBox.title = "温馨提示";
            this.$refs.tipsBox.tipContent=tipContent;
        }
    },
    closeTips(){
        if (this.$refs.tipsBox) {
            // 显示当前详情
            this.$refs.tipsBox.tkIsShow = false;
        }
    },
    confirmTip(){
        this.delGoodsImage();
    },
    delGoodsImage(){
        var url="manager/delGoodsImage";
        var params={
            id: this.editForm.id
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
                this.$message.success(res.msg)
                this.getData();
                this.closeTips();
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    edit(id,str){
        if(str=='add'){
            if (this.$refs.addGoodsPic) {
                this.$refs.addGoodsPic.tkIsShow = true;
                this.$refs.addGoodsPic.title = "新增图片";
                this.$refs.addGoodsPic.__init("add",0,this.spuId,this.type);
            }
        }else{
            if (this.$refs.addGoodsPic) {
                this.$refs.addGoodsPic.tkIsShow = true;
                this.$refs.addGoodsPic.title = "编辑图片";
                this.$refs.addGoodsPic.__init("edit",id,this.spuId,this.type);
            }
        }
    },
    handleSelectChange(){
        this.getData();
    }
  }
}
</script>
<style scoped lang="scss">
  .zhezhao{
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.2;
    top: 0;
    left: 0;
    z-index: 100;
  }
  .tankuang{
    position: fixed;
    width: 50%;
    height: 60%;
    background-color: #fff;
    top: 15%;
    left: 25%;
    z-index: 101;
    border-radius: 5px;
  }
  .tk_title{
    font-size: 17px;
    margin: 15px 0;
    height: 25px;
    line-height: 25px;
  }
  .tk_content{
    width: 90%;
    margin-left: 5%;
    height: calc(100% - 55px);
  }
  .tk_close{
    position: absolute;
    width: 16px;
    height: 16px;
    top: 18px;
    right: 20px;
  }
  .addBTN{
    position: absolute;
    top: 10px;
    left: 2%;
  }
  ::v-deep .el-select>.el-input{
    width: 150px !important;
  }
</style>